package com.twxm.breed.service;

import com.aliyuncs.exceptions.ClientException;
import com.twxm.breed.entity.CategoryInfo;
import com.twxm.breed.entity.DeviceInfo;
import com.twxm.breed.iot.ALiYunIOTAPI;
import com.twxm.breed.mapper.DeviceInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class DeviceInfoService {
    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

    public List<DeviceInfo> getAll() {
        return deviceInfoMapper.getAll();
    }

    public void setStatusOpen(String deviceId) {
        deviceInfoMapper.setStatusOpen(deviceId);
    }

    public void setStatusClose(String deviceId) {
        deviceInfoMapper.setStatusClose(deviceId);
    }

    public CategoryInfo getcategoryInfo(String deviceId) {
        return deviceInfoMapper.getcategoryInfo(deviceId);
    }

    public DeviceInfo selectById(String deviceId) {
        return deviceInfoMapper.selectDeviceById(deviceId);
    }

    public DeviceInfo getOxygenSensorByPondId(String pondId) {
        return deviceInfoMapper.getOxygenDeviceByPondId(pondId);
    }

    public int deleteDevice(String deviceId) {
        return deviceInfoMapper.deleteDevice(deviceId);
    }

    public void deleteDevices(List<String> list) throws ClientException {
        for (String s:list) {
            DeviceInfo deviceInfo = selectById(s);
            String productKey = deviceInfo.getCategoryInfo().getProductKey();
            ALiYunIOTAPI.getInstance().deleteDevice(productKey, deviceInfo.getDeviceId());
            deleteDevice(deviceInfo.getDeviceId());
        }
    }

    public List<String> selectIdByPondId(String pondId) {
        return deviceInfoMapper.selectIdByPondId(pondId);
    }

    public DeviceInfo getWaterSensorByPondId(String pondId) {
        return deviceInfoMapper.getWaterSensorByPondId(pondId);
    }

    public DeviceInfo getWeatherSensorByPondId(String pondId) {
        return deviceInfoMapper.getWeatherSensorByPondId(pondId);
    }

    public void setMinutes(Map<String, Object> map) {
        deviceInfoMapper.setMinutes(map);
    }
}
